﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace LabaCS4
{
    public partial class Main : Form
    {

        private int childFormNumber = 1;
        public Book bufbook;
        public Book cbook;

        private ArrayList arayTheBook;
        private Show childForm1;
       

        public Main()
        {
            InitializeComponent();
        }

        private void отрытьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog myOpenFileDialog = new OpenFileDialog();
            myOpenFileDialog.InitialDirectory = ".";
            myOpenFileDialog.Filter = "book files (*.book)|*.book|All files(*.*)|*.*";
            myOpenFileDialog.FilterIndex = 1;
            myOpenFileDialog.RestoreDirectory = true;

            if (myOpenFileDialog.ShowDialog() == DialogResult.OK)
            {

                Stream myStream = null;
                if ((myStream = myOpenFileDialog.OpenFile()) != null)
                {

                    BinaryFormatter myBinaryFormat = new BinaryFormatter();
                    arayTheBook = (ArrayList)myBinaryFormat.Deserialize(myStream);
                    myStream.Close();


                }

                Show childForm = new Show();
                childForm.MdiParent = this;
                childForm.main = this;
                childForm.Show();
                childForm.arTheBook = arayTheBook;
                childForm1 = childForm;
                childForm1.UpdateGrid();


                //очищаем текущий массив
                //arTheBook1.Clear();
            }
            

        }

        private void Form1_Load(object sender, EventArgs e)
        {
           

        }

        private void создатьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Show childForm = new Show();

            сохранитьToolStripMenuItem.Enabled = true;
            сохранитьКакToolStripMenuItem.Enabled = true;

            childForm.MdiParent = this;
            childForm.Text = "Каталог книг " + childFormNumber++;
            childForm.main = this;
            childForm.Show();
            arayTheBook = childForm.arTheBook;
            childForm1 = childForm;
        }

        private void сохранитьКакToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog mySaveFileDialog = new SaveFileDialog();
            mySaveFileDialog.InitialDirectory = ".";
            mySaveFileDialog.Filter="book files (*.book)|*.book|All files(*.*)|*.*";
            mySaveFileDialog.FilterIndex=1;
            mySaveFileDialog.RestoreDirectory=true;
            mySaveFileDialog.FileName="bookDoc";

            if(mySaveFileDialog.ShowDialog()== DialogResult.OK)
            {

                Stream myStream=null;
                if ((myStream=mySaveFileDialog.OpenFile()) != null)
                {
                                
                BinaryFormatter myBinaryFormat = new BinaryFormatter();
                myBinaryFormat.Serialize(myStream, arayTheBook);
                myStream.Close();


                }

            }

        }

        private void закрытьВсеToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void каскадToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void плиткаВертикToolStripMenuItem_Click(object sender, EventArgs e)
        {
           LayoutMdi(MdiLayout.TileVertical);
        }

        private void плиткаГоризToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void выходToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void вырезатьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Show frm = (Show)(this.ActiveMdiChild);
            frm.вырезатьToolStripMenuItem_Click(sender, e);

        }

        private void копироватьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Show frm = (Show)(this.ActiveMdiChild);
            //frm.копироватьToolStripMenuItem_Click(sender, e);

            bufbook = cbook;
         
        }

        private void вставитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Show frm = (Show)(this.ActiveMdiChild);
            frm.вставитьToolStripMenuItem_Click(sender, e);
          
        }

       

        private void удалитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Show frm = (Show)(this.ActiveMdiChild);
            frm.button3_Click(sender,e);
       

        }

        private void сохранитьToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}
